home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 July / Macworld (1999-07).dmg / Serious Software / OpenWorld demo 2.0 / Development / Utils / Debug / Debug.c next >
Text File  |  1999-01-13  |  6KB  |  238 lines

  1. //============================================================================
  2. //
  3. //    This is the Think C 6.0 code to send a message to the DebugWindow server
  4. //    (in other words, this is the code to DebugWindow.Lib that came with your
  5. //    DebugWindow package).  I'm providing this code to document the AppleEvent
  6. //    procedures necessary to communicate with DebugWindow; those of you who
  7. //    need a specialized version, or need to port it to another environment
  8. //    (such as Pascal or MacApp) should find everything that you need to know
  9. //    right here.
  10. //
  11. //    If you implement this into a new environment, I'd appreciate it if you
  12. //    could send me the new modules so that I can include them with the next
  13. //    release of DebugWindow (with the proper credits going to you, of course!).
  14. //
  15. //
  16. //    Here are the necessary steps to send a string to DebugWindow:
  17. //
  18. //        • create an AppleEvent for signature 'LdbW' with a type of 'misc/dmsg'
  19. //          (for a standard printf-type message), or 'misc/dmsh' for a hex dump.
  20. //
  21. //        • add a parameter of type 'keyDirectObject/typeChar' passing a pointer
  22. //          to the string to display and its length
  23. //
  24. //        • send it on its way
  25. //
  26. //
  27. //    Modification history
  28. //    --------------------
  29. //
  30. //    Date     Version                Changes
  31. //    --------   -------      ---------------------------------
  32. //    11-19-93    2.0            • Added the interface to the DebugHexDump
  33. //                              routine in DebugWindow.
  34. //
  35. //                            • Added the ability to bracket displays to the
  36. //                              DebugWindow with a BeginDebug() and EndDebug()
  37. //                              envelope.  If you are doing large numbers of
  38. //                              Debug() calls, this can dramatically speed
  39. //                              things up.  This does NOT add any benefit to
  40. //                              the DebugHexDump() call, since all of the logic
  41. //                              for that function is in DebugWindow itself.
  42. //
  43. //============================================================================
  44.  
  45.  
  46. #include    <stdio.h>
  47. #include    <stdarg.h>
  48. #include    <string.h>
  49. #include    <AppleEvents.h>
  50.  
  51. #define        MAX_STRING_SIZE        4096
  52. #define        CACHE_SIZE            8192        // size of display cache to allocate
  53.  
  54.  
  55.  
  56. void __SendToDebugWindow ( char *stringToSend, short length );
  57. void __FlushDebugWindowCache (void);
  58. void DebugCommand ( char *buffer, short length );
  59.  
  60.  
  61. short    __dbWinCount = 0;                    // counts number of BeginDebug calls
  62. char    *__dbCache  = nil;                    // holds our local display cache
  63. short    __cacheIndex = 0;                    // tail pointer for display cache
  64.  
  65.  
  66.  
  67. void Debug ( char *format, ... )
  68. {
  69.     va_list        argptr;
  70.     long        len;
  71.     OSErr        err;
  72.     //char        *tDebugString;
  73.     char        tDebugString[MAX_STRING_SIZE];
  74.  
  75.     //tDebugString = NewPtr ( MAX_STRING_SIZE );
  76.     if ( tDebugString ) {
  77.         va_start ( argptr, format );
  78.         len = (long)vsprintf ( tDebugString, format, argptr );
  79.         va_end ( argptr );
  80.         if ( len > 0 )
  81.             __SendToDebugWindow ( tDebugString, len );
  82.         //DisposPtr ( tDebugString );
  83.     }
  84. }
  85.  
  86.  
  87.  
  88.  
  89. void BeginDebug ()
  90. {
  91.     ++__dbWinCount;
  92. }
  93.  
  94.  
  95.  
  96.  
  97. void EndDebug ()
  98. {
  99.     --__dbWinCount;
  100.     if ( __dbWinCount == 0 )
  101.         __FlushDebugWindowCache();
  102. }
  103.  
  104.  
  105.  
  106.  
  107. void ClearDebugWindow ()
  108. {
  109.     DebugCommand ( "C", 1 );
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. void DebugTimestamp ()
  117. {
  118.     DebugCommand ( "T", 1 );
  119. }
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. void __SendToDebugWindow ( char *stringToSend, short msgLength )
  127. {
  128.     AEAddressDesc    address;
  129.     AppleEvent        appleEvent, reply;
  130.     OSType            targetSig;
  131.  
  132.  
  133.     if ( ! __dbCache )                                    // must be first time...
  134.         __dbCache = NewPtr ( CACHE_SIZE );
  135.  
  136.     if ( __dbCache ) {
  137.         if ( msgLength + __cacheIndex >= CACHE_SIZE )
  138.             __FlushDebugWindowCache();
  139.         memcpy ( __dbCache + __cacheIndex, stringToSend, msgLength );
  140.         __cacheIndex += msgLength;
  141.         if ( __dbWinCount == 0 )
  142.             __FlushDebugWindowCache();
  143.     }
  144. }
  145.  
  146.  
  147.  
  148.  
  149.  
  150. void DebugCommand ( char *buffer, short length )
  151. {
  152.     AEAddressDesc    address;
  153.     AppleEvent        appleEvent, reply;
  154.     OSType            targetSig;
  155.  
  156.     targetSig = 'LdbW';
  157.     if ( AECreateDesc ( typeApplSignature, (Ptr)&targetSig, 
  158.                         sizeof targetSig, &address ) == 0 ) {
  159.         if ( AECreateAppleEvent ( 'misc', 'dmsc', &address, kAutoGenerateReturnID,
  160.                                    kAnyTransactionID, &appleEvent ) == 0 ) {
  161.             if ( AEPutParamPtr ( &appleEvent, keyDirectObject, typeChar,
  162.                                  buffer, length ) == 0 ) {
  163.                 AESend ( &appleEvent, &reply, 
  164.                          kAEWaitReply + kAENeverInteract,
  165.                          kAENormalPriority, 
  166.                          300,                                 // up to 5 second wait..
  167.                          nil, nil );
  168.                 AEDisposeDesc ( &reply );
  169.             }
  170.             AEDisposeDesc ( &appleEvent );
  171.         }
  172.         AEDisposeDesc ( &address );
  173.     }
  174. }
  175.  
  176.  
  177.  
  178. void DebugHexDump ( char *buffer, short length )
  179. {
  180.     AEAddressDesc    address;
  181.     AppleEvent        appleEvent, reply;
  182.     OSType            targetSig;
  183.  
  184.     targetSig = 'LdbW';
  185.     if ( AECreateDesc ( typeApplSignature, (Ptr)&targetSig, 
  186.                         sizeof targetSig, &address ) == 0 ) {
  187.         if ( AECreateAppleEvent ( 'misc', 'dmsh', &address, kAutoGenerateReturnID,
  188.                                    kAnyTransactionID, &appleEvent ) == 0 ) {
  189.             if ( AEPutParamPtr ( &appleEvent, keyDirectObject, typeChar,
  190.                                  buffer, length ) == 0 ) {
  191.                 AESend ( &appleEvent, &reply, 
  192.                          kAEWaitReply + kAENeverInteract,
  193.                          kAENormalPriority, 
  194.                          300,                                 // up to 5 second wait..
  195.                          nil, nil );
  196.                 AEDisposeDesc ( &reply );
  197.             }
  198.             AEDisposeDesc ( &appleEvent );
  199.         }
  200.         AEDisposeDesc ( &address );
  201.     }
  202. }
  203.  
  204.  
  205.  
  206.  
  207.  
  208. void __FlushDebugWindowCache ()
  209. {
  210.     AEAddressDesc    address;
  211.     AppleEvent        appleEvent, reply;
  212.     OSType            targetSig;
  213.  
  214.     if ( __dbCache ) {
  215.         targetSig = 'LdbW';
  216.         if ( AECreateDesc ( typeApplSignature, (Ptr)&targetSig, 
  217.                             sizeof targetSig, &address ) == 0 ) {
  218.             if ( AECreateAppleEvent ( 'misc', 'dmsg', &address, kAutoGenerateReturnID,
  219.                                        kAnyTransactionID, &appleEvent ) == 0 ) {
  220.                 if ( AEPutParamPtr ( &appleEvent, keyDirectObject, typeChar,
  221.                                      __dbCache, __cacheIndex ) == 0 ) {
  222.                     AESend ( &appleEvent, &reply, 
  223.                              kAEWaitReply + kAENeverInteract,
  224.                              kAENormalPriority, 
  225.                              300,                                 // up to 5 second wait..
  226.                              nil, nil );
  227.                     AEDisposeDesc ( &reply );
  228.                 }
  229.                 AEDisposeDesc ( &appleEvent );
  230.             }
  231.             AEDisposeDesc ( &address );
  232.         }
  233.     }
  234.     __cacheIndex = 0;
  235. }
  236.  
  237.  
  238.